package controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cart.domain.Cart;
import cart.domain.CartItem;
import service.CommodityManager;
import vo.Commodity;

@Controller
public class CartController {
	
	@Resource(name="commodityService")
	private CommodityManager commodityService;
	
	@RequestMapping("/addCart")
	public String addCart(HttpServletRequest request, String commodityId,String count,Model model) {
		
		Cart cart = (Cart)request.getSession().getAttribute("cart");
		if(cart == null) {
			model.addAttribute("message", "请您登陆后订餐");
		}
		System.out.println("========================"+commodityId);
		Commodity commodity = commodityService.findCommodityById(commodityId);
		int _count = Integer.parseInt(count);
		CartItem cartItem = new CartItem();
		cartItem.setCommodity(commodity);
		cartItem.setCount(_count);
		
		cart.add(cartItem);
		request.getSession().setAttribute("cart", cart);
		return "cart";
	}
}
